import { ref, computed } from 'vue'
import { defineStore } from 'pinia'

export const useExamStore = defineStore('exam', () => {
    const showTime = ref(false) // 是否显示倒计时
    const examStart = ref(false) // 考试是否开始
    const reminder = ref(0) // 剩余时长 毫秒
    const currentExamIndex = ref(0) // 当前题目index
    const currentExam = ref({}) // 当前题目，用于刷新后回显

    const examFinished = ref(false) // 是否提交完成
    const timeoutIndex = ref(0) // 定时器对象，用于清除
    const answerTime = ref('') // 答题提交的用时

    // 基础试卷信息
    const paperInfo = ref({
        paperTotalScore: 0,
        questionTotalNum: 0,
        durationTime: 0,
        paperName: '',
        examStartTime: 0,
        examEndTime: 0,
        answerStartTime: '',
        answerEndTime: '',
    })

    // 用户答案
    const userAnswer = ref({})
    // 初始化用户答案
    let localAnswerList = JSON.parse(sessionStorage.getItem('userAnswer'))
    if (localAnswerList) {
        userAnswer.value = localAnswerList
    }
    let localCurrentExamIndex = JSON.parse(sessionStorage.getItem('currentExamIndex'))
        ? JSON.parse(sessionStorage.getItem('currentExamIndex'))
        : 0
    currentExamIndex.value = localCurrentExamIndex

    // getter，用于动态展示状态
    const getExamList = computed(() => examList.value)
    const getShowTime = computed(() => showTime.value)
    const getReminder = computed(() => reminder.value)
    const getExamStart = computed(() => examStart.value)
    const getCurrentExamIndex = computed(() => {
        sessionStorage.setItem('currentExamIndex', currentExamIndex.value)
        return currentExamIndex.value
    })

    // 获取当前题目
    const getCurrentExam = computed(() => {
        console.log('currentExamIndex', currentExamIndex.value)
        currentExam.value = examList.value[currentExamIndex.value]
        console.log('currentExam', currentExam.value)
        return currentExam.value
    })
    const getTestCardInfo = computed(() => testCardInfo.value)

    const examList = ref([]) // 试题列表
    const testCardInfo = ref([]) // 答题卡信息

    // 设置用户的答案，并写入缓存
    function setAnswer(answer) {
        userAnswer.value[answer.eqId] = answer.eqoId
        sessionStorage.setItem('userAnswer', JSON.stringify(userAnswer.value))
    }

    function clearUserAnswer() {
        userAnswer.value = {}
        sessionStorage.removeItem('userAnswer')
    }

    // 获取当前试题的答案，用于本地回显
    function getCurrentAnswer(eqId) {
        return userAnswer.value[eqId]
    }

    // 清空所有timeout
    function cleanTimeout() {
        for (var i = 0; i <= timeoutIndex.value; i++) {
            clearTimeout(timeoutIndex.value)
        }
    }

    // 初始化答卷信息
    function initExamListData(examData) {
        console.log('examData', examData)

        paperInfo.value.paperTotalScore = examData.paperTotalScore
        paperInfo.value.questionTotalNum = examData.questionTotalNum
        paperInfo.value.paperName = examData.paperName
        paperInfo.value.examStartTime = examData.examStartTime
        paperInfo.value.examEndTime = examData.examEndTime
        paperInfo.value.durationTime = examData.durationTime

        reminder.value = examData.durationTime * 60 * 1000

        examList.value = [].concat(
            examData.danxuanAnswers ? examData.danxuanAnswers : [],
            examData.duoxuanAnswers ? examData.duoxuanAnswers : [],
            examData.panduanAnswers ? examData.panduanAnswers : [],
            examData.tiankongAnswers ? examData.tiankongAnswers : [],
            examData.jiandaAnswers ? examData.jiandaAnswers : [],
            examData.zongheAnswers ? examData.zongheAnswers : [],
        )

        console.log('examList', examList)
        testCardInfo.value = [].concat(
            examData.danxuanTypeVO ? examData.danxuanTypeVO : [],
            examData.duoxuanTypeVO ? examData.duoxuanTypeVO : [],
            examData.panduanTypeVO ? examData.panduanTypeVO : [],
            examData.tiankongTypeVO ? examData.tiankongTypeVO : [],
            examData.jiandaTypeVO ? examData.jiandaTypeVO : [],
            examData.zongheTypeVO ? examData.zongheTypeVO : []
        )

        // 初始化当前题目
        currentExam.value = examList.value[currentExamIndex.value]
        console.log('test-card-info', testCardInfo)
    }

    return {
        timeoutIndex,
        answerTime,
        examList,
        reminder,
        showTime,
        examStart,
        examFinished,
        paperInfo,
        userAnswer,
        currentExamIndex,
        getShowTime,
        getReminder,
        getExamStart,
        getCurrentExamIndex,
        getCurrentExam,
        getTestCardInfo,
        setAnswer,
        getCurrentAnswer,
        getExamList,
        initExamListData,
        cleanTimeout,
        clearUserAnswer,
    }
})
